package org.oos.dao.options;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.oos.Constants;

import com.google.api.client.util.Key;

/**
 * 
 * This OptionMap defines the elements that shall be returned by the API in each
 * of the entries.
 * 
 * @author pablo.gonzalez@11870.com
 * 
 */
public class FieldOptions extends OptionsMap implements List<String> {

	@SuppressWarnings("unused")
	@Key
	private String fields;

	private List<String> fieldValues = new ArrayList<String>();

	public boolean add(String e) {
		if (fieldValues.add(e)) {
			update();
			return true;
		}
		return false;
	}

	public void clear() {
		fieldValues.clear();
	}

	public boolean isEmpty() {
		return fieldValues.isEmpty();
	}

	public int size() {
		return fieldValues.size();
	}

	public void add(int index, String element) {
		fieldValues.add(index, element);
		update();
	}

	public boolean addAll(Collection<? extends String> c) {
		if (fieldValues.addAll(c)) {
			update();
			return true;
		}
		return false;
	}

	public boolean addAll(int index, Collection<? extends String> c) {
		if (fieldValues.addAll(index, c)) {
			update();
			return true;
		}
		return false;
	}

	public boolean contains(Object o) {
		return fieldValues.contains(o);
	}

	public boolean containsAll(Collection<?> c) {
		return fieldValues.containsAll(c);
	}

	public boolean equals(Object o) {
		return fieldValues.equals(o);
	}

	public String get(int index) {
		return fieldValues.get(index);
	}

	public int hashCode() {
		return fieldValues.hashCode();
	}

	public int indexOf(Object o) {
		return fieldValues.indexOf(o);
	}

	public Iterator<String> iterator() {
		return fieldValues.iterator();
	}

	public int lastIndexOf(Object o) {
		return fieldValues.lastIndexOf(o);
	}

	public ListIterator<String> listIterator() {
		return fieldValues.listIterator();
	}

	public ListIterator<String> listIterator(int index) {
		return fieldValues.listIterator(index);
	}

	public boolean remove(Object o) {
		if (fieldValues.remove(o)) {
			update();
			return true;
		}
		return false;
	}

	public String remove(int index) {
		String previous = fieldValues.remove(index);
		update();
		return previous;
	}

	public boolean removeAll(Collection<?> c) {
		if (fieldValues.removeAll(c)) {
			update();
			return true;
		}
		return false;
	}

	public boolean retainAll(Collection<?> c) {
		return fieldValues.retainAll(c);
	}

	public String set(int index, String element) {
		return fieldValues.set(index, element);
	}

	public List<String> subList(int fromIndex, int toIndex) {
		return fieldValues.subList(fromIndex, toIndex);
	}

	public Object[] toArray() {
		return fieldValues.toArray();
	}

	public <T> T[] toArray(T[] a) {
		return fieldValues.toArray(a);
	}

	private void update() {
		this.fields = toString();
	}

	@Override
	public String toString() {
		Iterator<String> iterator = iterator();
		String result = null;
		if (iterator.hasNext()) {
			result = iterator.next();
			while (iterator.hasNext()) {
				result += Constants.COMMA;
				result += iterator.next();
			}
		}
		return result;
	}

}
